$(document).ready(function () {
    /* 提交试题时验证
     * 对认知层次是否选择进行验证
     *对题干是否为空进行验证
     *对试题是否重复再验证一次
     */
    /*============================试题提交============================================*/
    // 第一站问答
    $("#firstStopSbumit").click(function () {
        var volidate=volidateAuthorizationForSubmit();
        if (volidate==false) {
            return false;
        }

        var category = $('#wenda-category option:selected').text();
        var stem = $("#acupoint-stem").val();
        //验证提干是否唯一
        var isTheUniques = isTheUniquesStem(category, stem);
        if (isTheUniques == false) {
            return false;
        }
        //验证分数是否正确
        var confirm = confirmScoreByClass(".score1");
        if (confirm == false) {
            return false;
        }
    });

    // 第二站常规化
    $("#secondStopSbumit").click(function () {
        var volidate=volidateAuthorizationForSubmit();
        if (volidate==false) {
            return false;
        }

        var stem = $("#acupoint-location-stem").val();
        var category = $('#categoryTwoUseful option:selected').text();
        var isTheUniques = isTheUniquesStem(category, stem);
        if (isTheUniques == false) {
            return false;
        }
        //验证分数是否正确
        var confirm = confirmScoreByClass(".score2");
        if (confirm == false) {
            return false;
        }

    });

    // 第二站客观化
    $("#thirdStopSbumit").click(function () {
        var volidate=volidateAuthorizationForSubmit();
        if (volidate==false) {
            return false;
        }

        var stem = $("#single-ploy-stem").val();
        var category = $('#categoryThreeUseful option:selected').text();
        var isTheUniques = isTheUniquesStem(category, stem);
        if (isTheUniques == false) {
            return false;
        }
        //验证分数是否正确
        var confirm = confirmScoreByClass(".score3");
        if (confirm == false) {
            return false;
        }
    });
    /*============================试题提交============================================*/

    /*============================试题提交前进行权限验证============================================*/
function  volidateAuthorizationForSubmit() {
    var flag = true;
    $.ajax({
        url: basePath.value+"/examination/volidateAuthorizationForSubmit",
        type: "GET",
        dataType: "json",
        success: function (obj) {
            if (obj.code != null && obj.code == 200) {
                layer.msg(obj.msg, {icon: 5, time: 2000});
                flag = false;
            }
        }
    });
    return flag;
}
    /*============================试题提交前进行权限验证============================================*/

    /* 获取状态值 */
    var status = $("input[name='status']").val();
    if (status != "") {
        if (status == '1001') {
            layer.msg('试题添加成功!', {
                icon: 1,
                time: 1000
            });
        } else if (status == '1002') {
            layer.msg('试题添加失败!', {
                icon: 2,
                time: 1000
            });
        } else {
            layer.msg('不可重复添加试题!', {
                icon: 2,
                time: 1000
            });
        }
    }
});


function isTheUniquesStem(category, stem) {
    var result = false;
    if (stem == '') {
        layer.confirm('试题题干不可为空！', {
            btn: ["确认"]
        });
        return result;//题干为空不发送请求
    }
    $.ajax({
        type: "POST",
        url: basePath.value + '/examination/isTheUniqueStem',
        data: {stem: stem, category: category},
        dataType:"JSON",
        async: false,
        success: function (data) {
            var status = data.status;
            if (status == '1002') {//若是试题题干重复提示更改
                layer.confirm('试题题干不可用，请您重新编写！', {
                    btn: ["确认"]
                });
            } else {
                result = true;
            }
        }
    });
    return result;
}

$(document).ready(function () {
    //输入框聚焦时若没选择分类提示其选择分类
    //第一站
    $('#wenda input').focus(function () {
    });
    //第二站常规化
    $('#acupoint-location input').focus(function () {
    });


//题干失去焦点时验证题干是否唯一
//第一站
    $("#acupoint-stem").blur(function () {
        var category = $('#wenda-category option:selected').text();
        var stem = $("#acupoint-stem").val();
        isTheUniquesStem(category, stem);
        layer.msg('试题题干可用！', {
            icon: 1,
            time: 3000
        });
    });
//第二站常规化
    $("#acupoint-location-stem").blur(function () {
        var category = $('#categoryTwoUseful option:selected').text();
        var stem = $("#acupoint-location-stem").val();
        isTheUniquesStem(category, stem);
        layer.msg('试题题干可用！', {
            icon: 1,
            time: 3000
        });
    });

//第二站客观化
    $("#single-ploy-stem").blur(function () {
        var category = $('#categoryThreeUseful option:selected').text();
        var stem = $("#single-ploy-stem").val();
        isTheUniquesStem(category, stem);
        layer.msg('试题题干可用！', {
            icon: 1,
            time: 3000
        });
    });
});


var wendaAcupointLocationCount = 0,wendaAnswerMayorCureCount=0
    ,acupointLocationAdultCount=0,acupointLocationPediatricCount=0,wendaNSFECCount=0,
    singlePloyCECCount=0,completePloyFSENCount=0;
/***************************************每次切换分类将增加次数重置****************************************/
$(document).ready(function () {
    $(".rdobox").click(function(){
        wendaNSFECCount = 0;
        wendaAcupointLocationCount = 0;
        wendaAnswerMayorCureCount = 0;
        acupointLocationAdultCount=0;
        acupointLocationPediatricCount=0;
        singlePloyCECCount = 0;
        completePloyFSENCount = 0;
    });
});
/***************************************每次切换分类将增加次数重置****************************************/
/*============================增加主治或者答案============================================*/

function addTerms(divId,count,msg,category) {
    switch (category) {
        //增加-问答题-腧穴题-定位
        case "wendaAcupointLocation":
            if (wendaAcupointLocationCount > count) {
                // 询问框
                layer.confirm(msg, {
                    btn: ["确认"]
                    // 按钮
                });
            } else {
                wendaAcupointLocationCount = wendaAcupointLocationCount + 1;
                var content = "<div class='form-group'><div class='col-sm-2'></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + wendaAcupointLocationCount + "].answer'placeholder='请输入定位答案......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score1' name='commonAnswerList[" + wendaAcupointLocationCount + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'><i onclick='deleteNewAdd(this,\"wendaAcupointLocationCount\")' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div></div>";
                $(divId).append(content);
            }
            break;
        //增加-问答题-腧穴题-主治
        case "wendaAcupointMayorCure":
            if (wendaAnswerMayorCureCount > count) {
                // 询问框
                layer.confirm(msg, {
                    btn: ["确认"]
                    // 按钮
                });
            } else {
                wendaAnswerMayorCureCount = wendaAnswerMayorCureCount + 1;
                var content = "<div class='form-group'><div class='col-sm-2'></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + wendaAnswerMayorCureCount + "].answer'placeholder='请输入定位答案......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score1' name='commonAnswerList[" + wendaAnswerMayorCureCount + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'><i onclick='deleteNewAdd(this,\"wendaAnswerMayorCureCount\")' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div></div>";
                $(divId).append(content);
            }
            break;
        //增加-问答题-腧穴题-NSFEC
        case "wendaNSFEC":
        if (wendaNSFECCount > count) {
            // 询问框
            layer.confirm(msg, {
                btn: ["确认"]
                // 按钮
            });
        } else {
            wendaNSFECCount = wendaNSFECCount + 1;
            var content = "<div class='form-group'><div class='col-sm-2'></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + wendaNSFECCount + "].answer'placeholder='请输入答案......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score1' name='commonAnswerList[" + wendaNSFECCount + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'><i onclick='deleteNewAdd(this,\"wendaNSFECCount\")' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div></div>";
            $(divId).append(content);
        }
        break;
        //增加-穴位定位-成人穴位-定位
        case "acupointLocationAdultLocation":
            debugger
            if (acupointLocationAdultCount > count) {
                // 询问框
                layer.confirm(msg, {
                    btn: ["确认"]
                    // 按钮
                });
            } else {
                acupointLocationAdultCount = acupointLocationAdultCount + 1;
                var content = "<div class='form-group'><div class='col-sm-2'></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + acupointLocationAdultCount + "].answer'placeholder='请输入定位答案......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score1' name='commonAnswerList[" + acupointLocationAdultCount + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'><i onclick='deleteNewAdd(this,\"acupointLocationAdultCount\")' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div></div>";
                $(divId).append(content);
            }
            break;
        //增加-穴位定位-儿科穴位-定位
        case "acupointLocationPediatricLocation":
            debugger
            if (acupointLocationPediatricCount > count) {
                // 询问框
                layer.confirm(msg, {
                    btn: ["确认"]
                    // 按钮
                });
            } else {
                acupointLocationPediatricCount = acupointLocationPediatricCount + 1;
                var content = "<div class='form-group'><div class='col-sm-2'></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + acupointLocationPediatricCount + "].answer'placeholder='请输入定位答案......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score1' name='commonAnswerList[" + acupointLocationPediatricCount + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'><i onclick='deleteNewAdd(this,\"acupointLocationPediatricCount\")' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div></div>";
                $(divId).append(content);
            }
            break;
        //增加-单项手法-CEC
        case "singlePloyCEC":
            if (singlePloyCECCount > count) {
                // 询问框
                layer.confirm(msg, {
                    btn: ["确认"]
                    // 按钮
                });
            } else {
                singlePloyCECCount = singlePloyCECCount + 1;
                var content = "<div class='form-group'><div class='col-sm-2'></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + singlePloyCECCount + "].answer'placeholder='请输入答案......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score1' name='commonAnswerList[" + singlePloyCECCount + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'><i onclick='deleteNewAdd(this,\"singlePloyCECCount\")' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div></div>";
                $(divId).append(content);
            }
            break;
        //增加-成套手法-FSEN
        case "completePloyFSEN":
            if (completePloyFSENCount > count) {
                // 询问框
                layer.confirm(msg, {
                    btn: ["确认"]
                    // 按钮
                });
            } else {
                completePloyFSENCount = completePloyFSENCount + 1;
                var content = "<div class='form-group'><div class='col-sm-2'></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + completePloyFSENCount + "].answer'placeholder='请输入答案......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score1' name='commonAnswerList[" + completePloyFSENCount + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'><i onclick='deleteNewAdd(this,\"completePloyFSENCount\")' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div></div>";
                $(divId).append(content);
            }
            break;

        default:
        // ...
    }
}
/*============================增加主治或者答案============================================*/

/**************************************************************删除新增项****************************************************/
function deleteNewAdd(obj, countName) {
    $(obj).parent().parent().remove();
    switch (countName) {
        case "wendaAcupointLocationCount":
            wendaAcupointLocationCount = wendaAcupointLocationCount - 1;
            break;
        case "wendaAnswerMayorCureCount":
            wendaAnswerMayorCureCount = wendaAnswerMayorCureCount - 1;
            break;
        case "wendaNSFECCount":
            wendaNSFECCount = wendaNSFECCount - 1;
        case "acupointLocationAdultCount":
            acupointLocationAdultCount = acupointLocationAdultCount - 1;
            break;
        case "acupointLocationPediatricCount":
            acupointLocationPediatricCount = acupointLocationPediatricCount - 1;
        case "singlePloyCECCount":
            singlePloyCECCount = singlePloyCECCount - 1;
            break;
        case "completePloyFSENCount":
            completePloyFSENCount = completePloyFSENCount - 1;
            break;
        default:
    }
}
/**************************************************************删除新增项****************************************************/



function addAnswerTermTwo() {
    if (c > 4) {
        // 询问框
        layer.confirm('答案项已达上限！', {
            btn: ["确认"]
            // 按钮
        });
    } else {
        c = c + 1;
        var content = "<div class='form-group '><div class='col-sm-2'><i onclick='deleteNewAddC(this)' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + c + "].answer'placeholder='请输入答案项......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score2' name='commonAnswerList[" + c + "].score' onblur=\"scoreOnblur(this)\" required/></div><div class='col-md-1'></div></div>";
        $("#answerTwo").append(content);
    }
}

function addAnswerTermThree() {
    if (d > 4) {
        // 询问框
        layer.confirm('答案项已达上限！', {
            btn: ["确认"]
            // 按钮
        });
    } else {
        d = d + 1;
        var content = "<div class='form-group '><div class='col-sm-2'><i onclick='deleteNewAddD(this)' class='glyphicon glyphicon-remove' style='margin-left: 80%;margin-top:12%;cursor: pointer;'></i></div><div class='col-sm-6'><input type='text' class='form-control' name='commonAnswerList[" + d + "].answer'placeholder='请输入答案项......' required/></div><label class='col-sm-1 control-label' style='padding-left:10px'>分值</label><div class='col-sm-2'><input type='number' class='form-control score3' disabled/></div><div class='col-md-1'></div></div>";
        $("#answerThree").append(content);
    }
}

/*============================增加主治或者答案============================================*/

/**
 *@Description: 验证分数:如果分数超出范围，进行提示
 *@Date: 15:06 2017/8/9
 */
/**
 *@Description: 进行提交是进行验证
 *@Date: 16:10 2017/8/9
 */
var scoreResult;

/**
 * 验证分数
 * @param scoreClass
 * @returns {boolean}
 */
function confirmScoreByClass(scoreClass) {
    scoreResult = true;
    var score = $(scoreClass);
    for (i = 0; i < score.length; i++) {
        if (score[i].value < 0 || score[i].value > 30) {
            layer.msg('分数在0~30之间，请填写正确的分数!', {icon: 2, time: 2000});
            scoreResult = false;
        }
    }
    return scoreResult;
}

/**
 *@Description: 分数输入狂失去焦点时，进行分数验证
 *@Date: 16:10 2017/8/9
 */
function scoreOnblur(scoreClass) {
    var score = $(scoreClass).val();
    if (score < 0 || score > 30) {
        layer.msg('分数在0~30之间，请填写正确的分数!', {icon: 2, time: 2000});
    }
}